<?php namespace App\Utils\Sms;

use TopClient;
use AlibabaAliqinFcSmsNumSendRequest;
use AlibabaAliqinFcTtsNumSinglecallRequest;
use Illuminate\Support\Facades\Redis;
use Exception;
use Log;

class ALiDaYuSmsApi {
    //发送验证码
    //type属性
    //mode: 1短信验证码，2语音验证码
    //0发送失败，1发送成功，2等待60秒
    public static function getCodeInConsoleWeb($mobile) {
        try {
            $codeTtl = Redis::ttl('ConsoleWeb:SmsMobile:' . $mobile);
            if ($codeTtl) {
                if ($codeTtl > 540) {
                    return '2'; //0发送失败，1发送成功，2等待60秒
                }
            }
            $code = '';
            for ($i = 1; $i <= 6; $i++) {
                $code = $code . rand(0, 9);
            }
            $c = new TopClient;
            $c->appkey = env('ALIDAYU_APPKEY');
            $c->secretKey = env('ALIDAYU_SECRETKEY');
            $req = new AlibabaAliqinFcSmsNumSendRequest;
            $req->setExtend("123456");
            $req->setSmsType("normal");
            $req->setSmsFreeSignName("翻贝网");
            $req->setSmsParam("{\"code\":\"" . $code . "\",\"product\":\"翻贝网\"}");
            $req->setRecNum($mobile);
            $req->setSmsTemplateCode("SMS_105775012");
            $resp = $c->execute($req);
            if ($resp->code) {
                return '0';
            } else {
                Redis::setex('ConsoleWeb:SmsMobile:' . $mobile, 600, $code);
                return '1';
            }
        } catch(Exception $e) {
            Log::INFO("手机发送短信错误 ". $mobile);
            return '1';
        }
    }
    //验证验证码
    public static function verifyCodeInConsoleWeb($mobile,$code) {
        if(!$mobile || !$code) {
            return false;
        }
        if($code == Redis::get('ConsoleWeb:SmsMobile:'.$mobile)) {
            Redis::del('ConsoleWeb:SmsMobile:'.$mobile);
            return true;
        } else {
            return false;
        }
    }

    public static function collectMoneyToShop($mobile,$money) {
        try {
            if (env('WEB_URL') != 'https://www.fbw-china.com/') {
                return true;
            }
            $c = new TopClient;
            $c->appkey = env('ALIDAYU_APPKEY');
            $c->secretKey = env('ALIDAYU_SECRETKEY');
            $req = new AlibabaAliqinFcSmsNumSendRequest;
            $req->setExtend("123456");
            $req->setSmsType("normal");
            $req->setSmsFreeSignName("翻贝网");
            $req->setSmsParam("{\"money\":\"" . $money . "\"}");
            $req->setRecNum($mobile);
            $req->setSmsTemplateCode("SMS_34870048");
            $resp = $c->execute($req);
            return true;
        } catch(Exception $e) {
            return false;
        }
    }
    //mode 0表示第一入住，1表示续约，bankcard只提取后四位
    public static function firstRunToShop($mobile,$shopName,$cardholder,$bankcard,$mode = 0) {
        try {
            //正式环境或手机号码是邓志强的手机号码
            if ((env('WEB_URL') != 'https://www.fbw-china.com/') && ($mobile != '15070858455')) {
                return true;
            }
            $c = new TopClient;
            $c->appkey = env('ALIDAYU_APPKEY');
            $c->secretKey = env('ALIDAYU_SECRETKEY');
            $req = new AlibabaAliqinFcSmsNumSendRequest;
            $req->setExtend("123456");
            $req->setSmsType("normal");
            $req->setSmsFreeSignName("翻贝网");
            $req->setSmsParam("{\"shopname\":\"" . $shopName . "\",\"cardholder\":\"" . $cardholder . "\",\"bankcard\":\"" . $bankcard . "\"}");
            $req->setRecNum($mobile);
            if($mode == 0) {
                $req->setSmsTemplateCode("SMS_64535035");
            }
            else {
                $req->setSmsTemplateCode("SMS_64580023");
            }
            $resp = $c->execute($req);
            return true;
        } catch(Exception $e) {
            return false;
        }
    }

    public static function accountToShop($mobile,$shopName,$money,$bankNumber) {
        try {
            if (env('WEB_URL') != 'https://www.fbw-china.com/') {
                return true;
            }
            $c = new TopClient;
            $c->appkey = env('ALIDAYU_APPKEY');
            $c->secretKey = env('ALIDAYU_SECRETKEY');
            $req = new AlibabaAliqinFcSmsNumSendRequest;
            $req->setExtend("123456");
            $req->setSmsType("normal");
            $req->setSmsFreeSignName("翻贝网");
            $req->setSmsParam("{\"money\":\"" . $money . "\",\"shopname\":\"" . $shopName . "\",\"banknumber\":\"" . $bankNumber . "\"}");
            $req->setRecNum($mobile);
            $req->setSmsTemplateCode("SMS_48320121");
            $resp = $c->execute($req);
            return true;
        } catch(Exception $e) {
            return false;
        }
    }
}
?>